package com.rykj.ruiyan_ptz.wiget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Build
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import androidx.annotation.RequiresApi
import com.rykj.socket_lib.entity.BoundingBox

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class TargetPointView
@JvmOverloads
constructor(context: Context, attrs: AttributeSet, defStyle: Int = 0, defStyleRes: Int = 0) :
    View(context, attrs, defStyle, defStyleRes) {
    //所有目标画笔
    private val mPaint = Paint()
    private val mCenterPoint = Paint();
    private var isDrawingTargetRect:Boolean = false

    //按下的位置
    var downX: Float? = null
    var downY: Float? = null

    //触摸事件最后一次的位置
    var lastX: Float? = null
    var lastY: Float? = null
    init {

        mPaint.color = Color.parseColor("#FF00FF57")
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeWidth = 3f

        mCenterPoint.color = Color.BLACK
        mCenterPoint.style = Paint.Style.STROKE
        mCenterPoint.strokeWidth = 1f
    }

    //绘制
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        drawVideoCentre(canvas)
        drawTarget(canvas)
//        if (!isShow) {
//            return
//        }
//        //如果正在跟踪目标,绘制跟踪目标
//        if (isFollowing) {
//            drawFollowPoint(canvas)
//        }
//        //如果正在框选目标，绘制框选矩形
//        if (isDrawingTargetRect) {
//            drawSelectTargetRect(canvas)
//        }
//
//        //如果没有跟踪目标，绘制所有目标
//        drawAllTargetPoints(canvas)
    }

    private fun drawTarget(canvas: Canvas?) {
        var box1 = BoundingBox()
        box1.xmax = 90
        box1.xmin = 60
        box1.ymin = 50
        box1.ymax = 80

        if (box1.xmax - box1.xmin <= 20) {
            canvas?.drawLines(
                floatArrayOf(
                    box1.xmax.toFloat(),
                    box1.ymin.toFloat(),
                    box1.xmin.toFloat(),
                    box1.ymin.toFloat(),
                    box1.xmax.toFloat(),
                    box1.ymax.toFloat(),
                    box1.xmin.toFloat(),
                    box1.ymax.toFloat()
                ), mPaint
            )
        } else {
            canvas?.drawLines(
                floatArrayOf(
                    box1.xmin.toFloat(), box1.ymin.toFloat(),
                    box1.xmin.toFloat() + 10, box1.ymin.toFloat(),
                    box1.xmax.toFloat() - 10, box1.ymax.toFloat(),
                    box1.xmax.toFloat(), box1.ymax.toFloat(),
                    box1.xmax.toFloat() - 10, box1.ymin.toFloat(),
                    box1.xmax.toFloat(), box1.ymin.toFloat(),
                    box1.xmin.toFloat(), box1.ymax.toFloat(),
                    box1.xmin.toFloat() + 10, box1.ymax.toFloat()
                ), mPaint
            )
        }
        if (box1.ymax - box1.ymin <= 20) {
            canvas?.drawLines(
                floatArrayOf(
                    box1.xmin.toFloat(), box1.ymin.toFloat(),
                    box1.xmin.toFloat(), box1.ymax.toFloat(),
                    box1.xmax.toFloat(), box1.ymin.toFloat(),
                    box1.xmax.toFloat(), box1.ymax.toFloat()
                ), mPaint
            )
        } else {
            canvas?.drawLines(
                floatArrayOf(
                    box1.xmin.toFloat(), box1.ymin.toFloat(),
                    box1.xmin.toFloat(), box1.ymin.toFloat() + 10,
                    box1.xmin.toFloat(), box1.ymax.toFloat(),
                    box1.xmin.toFloat(), box1.ymax.toFloat() - 10,
                    box1.xmax.toFloat(), box1.ymin.toFloat() + 10,
                    box1.xmax.toFloat(), box1.ymin.toFloat(),
                    box1.xmax.toFloat(), box1.ymax.toFloat() - 10,
                    box1.xmax.toFloat(), box1.ymax.toFloat()
                ), mPaint
            )
        }
    }

    private fun drawVideoCentre(canvas: Canvas?) {
        mCenterPoint.color = Color.RED
        mCenterPoint.strokeWidth = 1F
        canvas?.drawLines(
            floatArrayOf(
                (this.width / 2 - 20).toFloat(),
                (this.height / 2).toFloat(),
                (this.width / 2 - 10).toFloat(),
                (this.height / 2).toFloat(),
                (this.width / 2).toFloat(),
                (this.height / 2 - 10).toFloat(),
                (this.width / 2).toFloat(),
                (this.height / 2 - 20).toFloat(),
                (this.width / 2 + 20).toFloat(),
                (this.height / 2).toFloat(),
                (this.width / 2 + 10).toFloat(),
                (this.height / 2).toFloat(),
                (this.width / 2).toFloat(),
                (this.height / 2 + 10).toFloat(),
                (this.width / 2).toFloat(),
                (this.height / 2 + 20).toFloat(),
            ), mPaint
        )
        mCenterPoint.color = Color.parseColor("#FFFF3D3D")
        mCenterPoint.strokeWidth = 6F
        canvas?.drawCircle(
            (this.width / 2).toFloat(),
            (this.height / 2).toFloat(),
            2.5f,
            mCenterPoint
        )
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        return super.onTouchEvent(event)
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                isDrawingTargetRect = true
                downX = event.x
                downY = event.y
            }
            MotionEvent.ACTION_UP -> {
                isDrawingTargetRect = false
            }

        }
    }

    @Synchronized
    fun updateRectTarget(points: List<BoundingBox>) {
//        isFollowing = false
//        followTargetPoints.clear()
//        if (!isShow || isFollowing || isDrawingTargetRect) {
//            return
//        }
////        if (mWidthScaling == 0.0 || mHeightScaling == 0.0) {
////            return
////        }
//        allTargetPoints = points
//        // todo 根据当前屏幕分辨率缩放坐标
//        allZoomTargetPoints.clear()
//        val x = (1280 - nowVideoWidth) / 2
//        val y = (720 - nowVideoHeight) / 2
//        RYLogger.i("lh", "nowVideoWidth ---> $nowVideoWidth , nowVideoHeight ---> $nowVideoHeight")
//        for (rectPoints in allTargetPoints) {
//
//            var xmin =
//                (rectPoints.xmin / 1280.0 * nowVideoWidth + x) * mWidthScaling - mWidthDifference
//
//            var ymin =
//                (rectPoints.ymin / 720.0 * nowVideoHeight + y) * mHeightScaling
//
//            var xmax =
//                (rectPoints.xmax / 1280.0 * nowVideoWidth + x) * mWidthScaling - mWidthDifference
//
//            var ymax =
//                (rectPoints.ymax / 720.0 * nowVideoHeight + y) * mHeightScaling - mHeightDifference
//
//            ymin = if (ymin < 0.0) 0.0 else ymin
//            ymax = if (ymax > measuredHeight) measuredHeight.toDouble() else ymax
//
//            xmin = if (xmin < 0.0) 0.0 else xmin
//            xmax = if (xmax > measuredWidth) measuredWidth.toDouble() else xmax
//
//            allZoomTargetPoints.add(arrayOf(xmin, ymin, xmax, ymax))
//        }

        invalidate()
    }
}